package com.gm.wholesale.module.good.bean;

import android.text.TextUtils;

import com.gm.commonlib.util.jsonUtils.JsonUtils;
import com.gm.commonlib.util.numberUtils.DigitUtil;
import com.gm.commonlib.util.numberUtils.NumberUtils;
import com.gm.wholesale.constant.Constant;
import com.gm.wholesale.db.order.bean.DBOrderContentBean;
import com.gm.wholesale.module.custom.ChoiceCustomData;
import com.gm.wholesale.module.custom.bean.CustomBean;
import com.gm.wholesale.module.good.GoodType;
import com.gm.wholesale.module.order.bean.AddWeightBean;
import com.gm.wholesale.module.unit.UnitType;
import com.gm.wholesale.module.unit.bean.UnitBean;
import com.gm.wholesale.util.Utils;
import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.Until;

import java.io.Serializable;
import java.util.List;


public class GoodBean extends GoodListBean implements Serializable {


    public List<AddWeightBean> addWeightBeans;

    public List<AddWeightBean> getAddWeightBeans() {
        return addWeightBeans;
    }

    public void setAddWeightBeans(List<AddWeightBean> addWeightBeans) {
        this.addWeightBeans = addWeightBeans;
    }

    private boolean isNewGood;

    public boolean isNewGood() {
        return isNewGood;
    }

    public void setNewGood(boolean newGood) {
        isNewGood = newGood;
    }

    private List<GoodBean> mSearchGoods;

    public List<GoodBean> getSearchGoods() {
        return mSearchGoods;
    }

    public void setSearchGoods(List<GoodBean> mSearchGoods) {
        this.mSearchGoods = mSearchGoods;
    }

    private String tempId;

    private boolean mIsUpdateView;

    private boolean mIsVoiceCreate;
    @SerializedName("orderLineId")
    private Long orderLineId;

    public Long getOrderLineId() {
        return orderLineId;
    }

    public void setOrderLineId(Long orderLineId) {
        this.orderLineId = orderLineId;
    }

    public boolean isIsVoiceCreate() {
        return mIsVoiceCreate;
    }

    public String getTempId() {
        return tempId;
    }

    public void setTempId(String tempId) {
        this.tempId = tempId;
    }

    public void setIsVoiceCreate(boolean mIsVoiceCreate) {
        this.mIsVoiceCreate = mIsVoiceCreate;
    }

    public boolean isIsUpdateView() {
        return mIsUpdateView;
    }

    public void setIsUpdateView(boolean mIsUpdateView) {
        this.mIsUpdateView = mIsUpdateView;
    }



    private boolean isTemporaryGood;

    public boolean isTemporaryGood() {
        return isTemporaryGood;
    }

    public void setTemporaryGood(boolean temporaryGood) {
        isTemporaryGood = temporaryGood;
    }

    private double totalPrice;

    public double getTotalPrice() {
        return totalPrice;
    }

    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    private double price;
    private double goodNum;

    private String unitName;
    private long unitId;

    public List<GoodBean> getmSearchGoods() {
        return mSearchGoods;
    }

    public void setmSearchGoods(List<GoodBean> mSearchGoods) {
        this.mSearchGoods = mSearchGoods;
    }

    public boolean ismIsUpdateView() {
        return mIsUpdateView;
    }

    public void setmIsUpdateView(boolean mIsUpdateView) {
        this.mIsUpdateView = mIsUpdateView;
    }

    public boolean ismIsVoiceCreate() {
        return mIsVoiceCreate;
    }

    public void setmIsVoiceCreate(boolean mIsVoiceCreate) {
        this.mIsVoiceCreate = mIsVoiceCreate;
    }

    public double getPrice() {
        return price;
    }
    public double getPriceByJin(){ //语音录单时候，获取单位为斤的价格
        //库存商品
        List<GoodListBean.UnitsBean> unitBeans=getUnits();
        if (unitBeans==null || unitBeans.isEmpty()){
            return 0;
        }
        for (GoodListBean.UnitsBean unitsBean:unitBeans){
            if (!TextUtils.isEmpty(unitsBean.getName())){
                if (unitsBean.getName().equals(Constant.DEFAULT_UNIT)
                && !TextUtils.isEmpty(unitsBean.getPrice())){
                     return Double.parseDouble(unitsBean.getPrice());
                }
            }
        }
        return 0;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getGoodNum() {
        return goodNum;
    }

    public void setGoodNum(double goodNum) {
        this.goodNum = goodNum;
    }

    public String getUnitName() {
        if (TextUtils.isEmpty(unitName)){
            return getDefaultGoodUnitName();
        }
        return unitName;
    }

    public String getDefaultGoodUnitName() {
        if (getType()==GoodType.UN_INVENTORY_GOOD){
            return "斤";
        }
        List<GoodListBean.UnitsBean> unitBeans=getUnits();
        if (unitBeans==null)
            return "斤";
        for (GoodListBean.UnitsBean unitsBean:unitBeans){
            if (unitsBean.getType()== UnitType.INVENTORY_UNIT){
                return unitsBean.getName();
            }
        }
        return "斤";
    }

    public void setUnitName(String unitName) {
        this.unitName = unitName;
    }

    public long getUnitId() {
        return unitId;
    }

    public void setUnitId(long unitId) {
        this.unitId = unitId;
    }

    public DBOrderContentBean toDBOrderBean() {
        DBOrderContentBean dbOrderContentBean = new DBOrderContentBean();
       // dbOrderContentBean.setCategoryId(categoryId);
       // dbOrderContentBean.setCategoryName(categoryName);
        dbOrderContentBean.setName(name);
        dbOrderContentBean.setPrice(price);
        dbOrderContentBean.setGoodNum(goodNum);

        dbOrderContentBean.setUnitId(unitId);
        dbOrderContentBean.setUnitName(getUnitName());

        dbOrderContentBean.setTempId(tempId);
        dbOrderContentBean.setTotalPrice(totalPrice);
        dbOrderContentBean.setGoodId(id);
        List<UnitsBean> unitsBeans=getUnits();
        if (unitsBeans!=null && !unitsBeans.isEmpty()){
            String units= JsonUtils.toString(unitsBeans);
            dbOrderContentBean.setOther1(units);
        }
        CustomBean customBean=ChoiceCustomData.newInstance().getCustomBean();
         if (customBean!=null){
             dbOrderContentBean.setCustomName(customBean.getName());
             dbOrderContentBean.setCustomId(customBean.getId());
             dbOrderContentBean.setContactPerson(customBean.getContactPerson());
             dbOrderContentBean.setContactPhone(customBean.getContactPhone());
         }
        return dbOrderContentBean;
    }

    public boolean isWeightUnit(){
         List<GoodListBean.UnitsBean> unitBeans=getUnits();
         //非库存单位默认斤
         if (getType()== GoodType.UN_INVENTORY_GOOD){
             return true;
         }
         else if (!TextUtils.isEmpty(unitName)){
             if (Utils.isWeightUnit(unitName)){
                 return true;
             }
             return false;
         }
         else {
             if (unitBeans==null)return false;
             for (GoodListBean.UnitsBean unitsBean:unitBeans){
                 if (unitsBean.getType()== UnitType.INVENTORY_UNIT){
                     if (Utils.isWeightUnit(unitsBean.getName())){
                         return true;
                     }
                     return false;
                 }
             }
         }
        return false;
    }
}
